<script lang="ts" setup>
import { getRuleListAPI } from '@/apis/car';
import type { Car, CarListParams } from '@/types/car';
import { ref } from 'vue';
import { utils, writeFileXLSX} from 'xlsx'

const ruleList = ref<Car[]>([])
const params = ref<CarListParams>({
  page: 1,
  pageSize: 10
})
const total = ref(0)

// 获取规则列表
const getRuleList = async () => {
  const res = await getRuleListAPI(params.value)
  ruleList.value = res.data.rows
  total.value = res.data.total
}
getRuleList()

const chargeType: any = {
  'duration': '时长收费',
  'turn': '按次收费',
  'partition': '分段消费'
}

// 1. 导出Excel
const exportExcel = ()=>{
  // 1.0 处理数据
  // 表头中文字段value（设置表头使用）
  const tableHeaderValues = ['编号ID', '计费规则编号', '计费规则名称', '免费时长(分钟)', '收费上线(元)', '计费方式', '计费规则']
  const tableHeaderKeys = ['id', 'ruleNumber', 'ruleName', 'freeDuration', 'chargeCeiling', 'chargeType', 'ruleNameView']
  const sheetData = ruleList.value.map((item: any)=>{
     const obj: any = {}
     tableHeaderKeys.forEach((key: string)=>{
       if (key === 'chargeType'){
         obj['chargeType'] = chargeType[item['chargeType']]
        }else{
          obj[key] = item[key]
        }
     })
    return obj
  })
  // ---------------------------
  // 1.1 创建一个工作簿
  const workbook = utils.book_new()
  // 1.2 创建一个工作表
  const worksheet = utils.json_to_sheet(sheetData)
  // 1.3 把工作表添加到工作簿, 并给工作表改名字
  utils.book_append_sheet(workbook, worksheet, '计费信息')
  // 1.4 改写表头
  utils.sheet_add_aoa(worksheet, [tableHeaderValues], {origin: 'A1'})
  // 1.5 导出工作簿
  writeFileXLSX(workbook, '计费规则表.xlsx')
}

</script>

<template>
  <div class="rule-container">
    <div class="create-container">
      <el-button type="primary">增加停车计费规则</el-button>
      <el-button @click="exportExcel">导出Excel</el-button>
    </div>
    <!-- 表格区域 -->
    <div class="table">
      <el-table :data="ruleList" style="width: 100%">
        <el-table-column type="index" label="序号" width="100" />
        <el-table-column label="计费规则编号" prop="ruleNumber" />
        <el-table-column label="计费规则名称" prop="ruleName" />
        <el-table-column label="免费时长(分钟)" prop="freeDuration" />
        <el-table-column label="收费上限(元)" prop="chargeCeiling" />
        <el-table-column label="计费方式" width="200">
          <template #default="scope">
            {{ chargeType[scope.row.chargeType] }}
          </template>
        </el-table-column>
        <el-table-column label="计费规则" prop="ruleNameView" width="200" />
        <el-table-column label="操作" fixed="right" width="200">
          <template #default>
            <el-button size="small" type="success">编辑</el-button>
            <el-button size="small" type="info">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
  </div>
</template>

<style lang="scss" scoped>
.rule-container {
  padding: 20px;
  background-color: #fff;
}

.rules .el-form-item {
  padding: 0 10px;
}

.search-container {
  display: flex;
  align-items: center;
  border-bottom: 1px solid rgb(237, 237, 237, .9);
  padding-bottom: 20px;

  .search-label {
    width: 100px;
  }

  .search-main {
    width: 220px;
    margin-right: 10px;
  }
}

.create-container {
  margin: 10px 0px;
}

.page-container {
  padding: 4px 0px;
  text-align: right;
}

.form-container {
  padding: 0px 80px;
}
</style>